{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Equal Sum Arrays With Minimum Number of Operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array #hash-table #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组 #哈希表 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minOperations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #通过最少操作次数使数组的和相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个长度可能不等的整数数组 <code>nums1</code> 和 <code>nums2</code> 。两个数组中的所有值都在 <code>1</code> 到 <code>6</code> 之间（包含 <code>1</code> 和 <code>6</code>）。</p>\n",
    "\n",
    "<p>每次操作中，你可以选择 <strong>任意</strong> 数组中的任意一个整数，将它变成 <code>1</code> 到 <code>6</code> 之间 <strong>任意</strong> 的值（包含 <code>1</code> 和 <code><span style=\"\">6</span></code>）。</p>\n",
    "\n",
    "<p>请你返回使 <code>nums1</code> 中所有数的和与 <code>nums2</code> 中所有数的和相等的最少操作次数。如果无法使两个数组的和相等，请返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]\n",
    "<b>输出：</b>3\n",
    "<b>解释：</b>你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。\n",
    "- 将 nums2[0] 变为 6 。 nums1 = [1,2,3,4,5,6], nums2 = [<strong>6</strong>,1,2,2,2,2] 。\n",
    "- 将 nums1[5] 变为 1 。 nums1 = [1,2,3,4,5,<strong>1</strong>], nums2 = [6,1,2,2,2,2] 。\n",
    "- 将 nums1[2] 变为 2 。 nums1 = [1,2,<strong>2</strong>,4,5,1], nums2 = [6,1,2,2,2,2] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums1 = [1,1,1,1,1,1,1], nums2 = [6]\n",
    "<b>输出：</b>-1\n",
    "<b>解释：</b>没有办法减少 nums1 的和或者增加 nums2 的和使二者相等。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums1 = [6,6], nums2 = [1]\n",
    "<b>输出：</b>3\n",
    "<b>解释：</b>你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。\n",
    "- 将 nums1[0] 变为 2 。 nums1 = [<strong>2</strong>,6], nums2 = [1] 。\n",
    "- 将 nums1[1] 变为 2 。 nums1 = [2,<strong>2</strong>], nums2 = [1] 。\n",
    "- 将 nums2[0] 变为 4 。 nums1 = [2,2], nums2 = [<strong>4</strong>] 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 6</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [equal-sum-arrays-with-minimum-number-of-operations](https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [equal-sum-arrays-with-minimum-number-of-operations](https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4,5,6]\\n[1,1,2,2,2,2]', '[1,1,1,1,1,1,1]\\n[6]', '[6,6]\\n[1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\r\n",
    "        if sum(nums1) == sum(nums2):\r\n",
    "            return 0 \r\n",
    "        elif sum(nums1) > sum(nums2):\r\n",
    "            return self.minOperations(nums2, nums1)\r\n",
    "    \r\n",
    "        cnt = Counter()\r\n",
    "        for x in nums1:\r\n",
    "            cnt[6 - x] += 1\r\n",
    "        for x in nums2:\r\n",
    "            cnt[x - 1] += 1\r\n",
    "        dif = sum(nums2) - sum(nums1)\r\n",
    "        res = 0 \r\n",
    "        for x in range(5, 0, -1):\r\n",
    "            if cnt[x] == 0:\r\n",
    "                continue\r\n",
    "            if cnt[x] * x >= dif:\r\n",
    "                res += (dif + x - 1) // x \r\n",
    "                return res  \r\n",
    "            dif -= cnt[x] * x \r\n",
    "            res += cnt[x] \r\n",
    "        return -1\r\n",
    "            \r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        sums1 = sum(nums1)\n",
    "        sums2 = sum(nums2)\n",
    "\n",
    "        if sums1 < sums2:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "        diff = abs(sums1-sums2)\n",
    "        if diff == 0:\n",
    "            return 0\n",
    "        nums1.sort(reverse=True)\n",
    "        nums2.sort()\n",
    "\n",
    "        i, j = 0, 0\n",
    "        res = 0\n",
    "        while i < len(nums1) or j < len(nums2):\n",
    "            sub = nums1[i]-1 if i < len(nums1) else float('-inf')\n",
    "            plus = 6-nums2[j] if j < len(nums2) else float('-inf')\n",
    "            if sub > plus:\n",
    "                diff -= nums1[i]-1\n",
    "                res += 1\n",
    "                i += 1\n",
    "                if diff <= 0:\n",
    "                    return res\n",
    "            else:\n",
    "                diff -= 6-nums2[j]\n",
    "                res += 1\n",
    "                j += 1\n",
    "                if diff <= 0:\n",
    "                    return res\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m, n = len(nums1), len(nums2)\n",
    "\n",
    "        if 6 * m < n or 6 * n < m:\n",
    "            return -1\n",
    "        \n",
    "        d = sum(nums2) - sum(nums1)\n",
    "        if d < 0:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "            d = -d\n",
    "        ans = 0\n",
    "        # nums1变大，nums2变小\n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "        \n",
    "        for i in range(5, 0, -1):\n",
    "            # 如果当前的做法能包住，需要向上取整\n",
    "            if d <= cnt[i] * i:\n",
    "                return ans + math.ceil(d / i)\n",
    "            # 否则全收了\n",
    "            ans += cnt[i]\n",
    "            d -= i * cnt[i]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #copy\n",
    "        if 6*len(nums1)<len(nums2) or 6*len(nums2)<len(nums1):\n",
    "            return -1\n",
    "\n",
    "        d= sum(nums2)-sum(nums1)\n",
    "        if d<0:\n",
    "            d =-d\n",
    "            nums1,nums2 = nums2,nums1     \n",
    "\n",
    "        ans = 0\n",
    "        cnt = Counter(6-x for x in nums1) + Counter(x-1 for x in nums2)\n",
    "\n",
    "        for i in range(5,0,-1):\n",
    "            if i *cnt[i]>=d:\n",
    "                return ans + (d+i-1)//i\n",
    "            ans += cnt[i]\n",
    "            d -= i*cnt[i]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #和相等，选择 任意 数组中的任意一个整数\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        if n1 < n2 and 6*n1<n2:\n",
    "            return -1\n",
    "        if n1 > n2 and 6*n2<n1:\n",
    "            return -1\n",
    "        sum1 = sum(nums1)\n",
    "        sum2 = sum(nums2)\n",
    "        d = sum(nums2) - sum(nums1)  # 数组元素和的差，我们要让这个差变为 0\n",
    "        if d == 0:\n",
    "            return 0\n",
    "        if d < 0:\n",
    "            d = -d\n",
    "            nums1, nums2 = nums2, nums1  # 统一让 nums1 的数变大，nums2 的数变小\n",
    "        ans = 0\n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "        # 统计每个数的最大变化量（nums1 的变成 6，nums2 的变成 1）\n",
    "\n",
    "        for i in range(5,0,-1):\n",
    "            if i*cnt[i] >= d:# 可以让 d 变为 0\n",
    "                return ans + (d+i-1)//i\n",
    "            ans += cnt[i]  # 需要所有最大变化量为 i 的数\n",
    "            d -= i * cnt[i]\n",
    "        \n",
    "\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if 6 * len(nums1) < len(nums2) or 6 * len(nums2) < len(nums1):\n",
    "            return -1\n",
    "        d = sum(nums2) - sum(nums1)  # 数组元素和的差，我们要让这个差变为 0\n",
    "        if d < 0:\n",
    "            d = -d\n",
    "            nums1, nums2 = nums2, nums1  # 统一让 nums1 的数变大，nums2 的数变小\n",
    "        \n",
    "        ans = 0\n",
    "        # 统计每个数的最大变化量（nums1 的变成 6，nums2 的变成 1）\n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "        for i in range(5, 0, -1):\n",
    "            if i * cnt[i] >= d:\n",
    "                return ans + (d + i - 1) // i\n",
    "            ans += cnt[i]\n",
    "            d -= i * cnt[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m = len(nums1)\n",
    "        n = len(nums2)\n",
    "        if 6*m < n or 6*n < m:\n",
    "            return -1\n",
    "        d = sum(nums2)-sum(nums1)\n",
    "        if d < 0:\n",
    "            d = -d\n",
    "        # nums1是变大,nums2是变小\n",
    "            nums1,nums2 = nums2,nums1\n",
    "        cnt = Counter(6-x for x in nums1)+Counter(x-1 for x in nums2)\n",
    "        ans = 0\n",
    "        # 枚举变换量\n",
    "        for i in range(5,0,-1):\n",
    "            if i*cnt[i] >= d:\n",
    "                return ans + (d+i-1) // i\n",
    "            ans += cnt[i]\n",
    "            d -= i*cnt[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if len(nums1) > 6*len(nums2) or len(nums2) > 6*len(nums1):\n",
    "            return -1\n",
    "        diff = sum(nums1) - sum(nums2)\n",
    "\n",
    "        if diff == 0:\n",
    "            return 0\n",
    "\n",
    "        if diff < 0:\n",
    "            diff *= -1\n",
    "            nums1,nums2 = nums2,nums1\n",
    "        # 排序好之后，nums1从最后一个开始遍历，因为nums1的和比较大，从6开始变为1\n",
    "        # nums2从0，开始，把1变成6\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        i = len(nums1) - 1\n",
    "        j = 0\n",
    "\n",
    "        res = 0\n",
    "        while diff > 0:\n",
    "            # 如果diff比5大，因为6变成1最大差值就是5，也就是这次循环不能完全减完，还有剩下的差值\n",
    "            if diff > 5:\n",
    "                # 每次比较nums1[i] - 1和6 - nums2[j]的差值d1,d2,变化比较大的那个\n",
    "                d1 = nums1[i] - 1\n",
    "                d2 = 6 - nums2[j]\n",
    "                if d1 > d2:\n",
    "                    # 相当于执行了这么一句\n",
    "                    # nums1[i] = 1\n",
    "                    diff -= d1\n",
    "                    # 因为nums1[i]变化过了，所以i--\n",
    "                    # 这里是为了防止index out of range\n",
    "                    i -= 1 if i != 0 else 0\n",
    "                else:\n",
    "                    # 同理\n",
    "                    # nums2[j] = 6\n",
    "                    diff -= d2\n",
    "                    # 这里是为了防止index out of range\n",
    "                    j += 1 if j != len(nums2) - 1 else 0\n",
    "                res += 1\n",
    "            # 如果diff小于5，也就是出口\n",
    "            else:\n",
    "                # 这里d1,d2还是最大变化的大小，只不过可以直接和diff比\n",
    "                d1 = nums1[i] - 1\n",
    "                d2 = 6 - nums2[j]\n",
    "                # 如果d1和diff的差值\n",
    "                # 如果diff > d1且diff > d2,因为，如果其中一个不成立还可以用另一个\n",
    "                # nums1[i] = 3,这里nums1的最大限度是变为1，也就是d1=2,要减少diff = 4则不可能\n",
    "                # nums2[j] = 3,这里nums2的最大限度是变为6，也就是d2=3,要增加diff = 4则不可能\n",
    "                # 只能分两步。但是我这里突然想到了，前面一个if，不是每一步都要减到1，或者加到6，这还能省一步，汗颜；不对，前面变化都是最快的，这里如果不能一步变化那也应该是合理的\n",
    "                if diff > d1 and diff > d2:\n",
    "                    if d1 > d2:\n",
    "                        diff -= d1\n",
    "                        i -= 1 if i != 0 else 0\n",
    "                    else:\n",
    "                        diff -= d2\n",
    "                        j += 1 if j != len(nums2) - 1 else 0\n",
    "                    res += 1\n",
    "                else:\n",
    "                    res += 1\n",
    "                    break\n",
    "        return res\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #和相等，选择 任意 数组中的任意一个整数\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        if n1 < n2 and 6*n1<n2:\n",
    "            return -1\n",
    "        if n1 > n2 and 6*n2<n1:\n",
    "            return -1\n",
    "        sum1 = sum(nums1)\n",
    "        sum2 = sum(nums2)\n",
    "        d = sum(nums2) - sum(nums1)  # 数组元素和的差，我们要让这个差变为 0\n",
    "        if d == 0:\n",
    "            return 0\n",
    "        if d < 0:\n",
    "            d = -d\n",
    "            nums1, nums2 = nums2, nums1  # 统一让 nums1 的数变大，nums2 的数变小\n",
    "        ans = 0\n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "\n",
    "        for i in range(5,0,-1):\n",
    "            if i*cnt[i] >= d:\n",
    "                return ans + (d+i-1)//i\n",
    "            ans += cnt[i]  # 需要所有最大变化量为 i 的数\n",
    "            d -= i * cnt[i]\n",
    "        \n",
    "\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if 6 * len(nums1) < len(nums2) or 6 * len(nums2) < len(nums1):\n",
    "            return -1 # 优化\n",
    "        d = sum(nums2) - sum(nums1)\n",
    "        if d < 0:\n",
    "            d = -d\n",
    "            nums1, nums2 = nums2, nums1 # 统一让nums1的数变大，nums2的数变小\n",
    "        ans = 0\n",
    "\n",
    "\n",
    "        # 统计每个数的最大变化量 nums1的变成6， nums2的变成1\n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "        for i in range(5, 0, -1): # 从大到小枚举最大变化量 5 4 3 2 1\n",
    "            if i * cnt[i] >= d: # 可以让d变为0\n",
    "                return ans + (i + d - 1) // i # 整除\n",
    "            ans += cnt[i] # 需要所有最大变化量为i的数\n",
    "            d -= i * cnt[i]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m, n = len(nums1), len(nums2)\n",
    "        if 6 * m < n or 6 * n < m:\n",
    "            return -1\n",
    "        \n",
    "        d = sum(nums2) - sum(nums1)\n",
    "        if d < 0:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "            d = -d\n",
    "        \n",
    "        ans = 0\n",
    "        # nums1变大，nums2变小\n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "\n",
    "        for i in range(5, 0, -1):\n",
    "            if cnt[i] * i >= d:\n",
    "                return ans + math.ceil(d * 1.0 / i)\n",
    "            ans += cnt[i]\n",
    "            d -= cnt[i] * i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        change, target, result, cur = [0] * 6, abs((s1 := sum(nums1)) - (s2 := sum(nums2))), 0, 5\n",
    "        if s1 < s2:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "        for num in nums1:\n",
    "            change[num - 1] += 1\n",
    "        for num in nums2:\n",
    "            change[6 - num] += 1\n",
    "        while target:\n",
    "            if not cur:\n",
    "                return -1\n",
    "            if target > change[cur] * cur:\n",
    "                result += change[cur]\n",
    "                target -= change[cur] * cur\n",
    "                cur -= 1\n",
    "            else:\n",
    "                return result + target // cur + (not not target % cur)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if 6*len(nums1)<len(nums2) or 6*len(nums2)<len(nums1):\n",
    "            return -1\n",
    "        if sum(nums1)<sum(nums2):\n",
    "            nums1,nums2 = nums2,nums1\n",
    "        d = sum(nums1)-sum(nums2)    \n",
    "        res = 0\n",
    "        mapping = {}\n",
    "        for i in range(0,6):\n",
    "            mapping[i] = 0\n",
    "        for i in nums1:\n",
    "            mapping[i-1] += 1\n",
    "        for i in nums2:\n",
    "            mapping[6-i] += 1\n",
    "        for i in range(5,0,-1):\n",
    "            if d<=i*mapping[i]:\n",
    "                return res+(d+i-1)//i\n",
    "            d-=i*mapping[i]\n",
    "            res+=mapping[i]\n",
    "                \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if 6*len(nums1)<len(nums2) or 6*len(nums2)<len(nums1):\n",
    "            return -1\n",
    "        if sum(nums1)<sum(nums2):\n",
    "            nums1,nums2 = nums2,nums1\n",
    "        d = sum(nums1)-sum(nums2)    \n",
    "        res = 0\n",
    "        mapping = {}\n",
    "        for i in range(0,6):\n",
    "            mapping[i] = 0\n",
    "        for i in nums1:\n",
    "            mapping[i-1] += 1\n",
    "        for i in nums2:\n",
    "            mapping[6-i] += 1\n",
    "        for i in range(5,0,-1):\n",
    "            if d<=i*mapping[i]:\n",
    "                return res+(d+i-1)//i\n",
    "            else:\n",
    "                d-=i*mapping[i]\n",
    "                res+=mapping[i]\n",
    "                \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        if n1*6 < n2 or n2*6 < n1:\n",
    "            return -1\n",
    "        \n",
    "        s1 = sum(nums1)\n",
    "        s2 = sum(nums2)\n",
    "        if s1==s2:\n",
    "            return 0\n",
    "        \n",
    "        count = 0\n",
    "        dic1 = {}\n",
    "        dic2 = {}\n",
    "        for num in range(6):\n",
    "            dic1[num+1] = 0\n",
    "            dic2[num+1] = 0\n",
    "        for num in nums1:\n",
    "            dic1[num] += 1\n",
    "        for num in nums2:\n",
    "            dic2[num] += 1\n",
    "        \n",
    "        if s1<s2:\n",
    "            for num in range(1,6):\n",
    "                while dic1[num]:\n",
    "                    dic1[num] -= 1\n",
    "                    dic1[6] += 1\n",
    "                    count += 1\n",
    "                    s1 += 6-num\n",
    "                    if s1>=s2:\n",
    "                        return count\n",
    "                while dic2[7-num]:\n",
    "                    dic2[7-num] -= 1\n",
    "                    dic2[1] += 1\n",
    "                    count += 1\n",
    "                    s2 -= 6-num\n",
    "                    if s1 >= s2:\n",
    "                        return count\n",
    "        if s1>s2:\n",
    "            for num in range(1,6):\n",
    "                while dic2[num]:\n",
    "                    dic2[num] -= 1\n",
    "                    dic2[6] += 1\n",
    "                    count += 1\n",
    "                    s2 += 6-num\n",
    "                    if s2>=s1:\n",
    "                        return count\n",
    "                while dic1[7-num]:\n",
    "                    dic1[7-num] -= 1\n",
    "                    dic1[1] += 1\n",
    "                    count += 1\n",
    "                    s1 -= 6-num\n",
    "                    if s2 >= s1:\n",
    "                        return count\n",
    "        \n",
    "        return -1\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #copy\n",
    "        if 6*len(nums1)<len(nums2) or 6*len(nums2)<len(nums1):\n",
    "            return -1\n",
    "\n",
    "        d= sum(nums2)-sum(nums1)\n",
    "        if d<0:\n",
    "            d =-d\n",
    "            nums1,nums2 = nums2,nums1     \n",
    "\n",
    "        ans = 0\n",
    "        cnt = Counter(6-x for x in nums1) + Counter(x-1 for x in nums2)\n",
    "\n",
    "        for i in range(5,0,-1):\n",
    "            if i *cnt[i]>=d:\n",
    "                return ans + (d+i-1)//i\n",
    "            ans += cnt[i]\n",
    "            d -= i*cnt[i]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        x=len(nums1)\n",
    "        y=len(nums2)\n",
    "        d1={6:0,5:0,4:0,3:0,2:0,1:0}\n",
    "        d2={6:0,5:0,4:0,3:0,2:0,1:0}\n",
    "        s1=0\n",
    "        s2=0\n",
    "        for i in nums1:\n",
    "            d1[i]+=1\n",
    "            s1+=i\n",
    "        for i in nums2:\n",
    "            d2[i]+=1\n",
    "            s2+=i\n",
    "        if s1==s2:\n",
    "            return 0\n",
    "        if x>6*y or y>6*x:\n",
    "            return -1\n",
    "        tot=0\n",
    "        if s1>s2:\n",
    "            g=s1-s2\n",
    "            for i in range(6,1,-1):\n",
    "                if g>d1[i]*(i-1):\n",
    "                    g-=d1[i]*(i-1)\n",
    "                    tot+=d1[i]\n",
    "                else:\n",
    "                    if g%(i-1)==0:\n",
    "                        tot+=g//(i-1)\n",
    "                    else:\n",
    "                        tot+=g//(i-1)+1\n",
    "                    return tot\n",
    "                if g>(i-1)*d2[7-i]:\n",
    "                    g-=d2[7-i]*(i-1)\n",
    "                    tot+=d2[7-i]\n",
    "                else:\n",
    "                    if g%(i-1)==0:\n",
    "                        tot+=g//(i-1)\n",
    "                    else:\n",
    "                        tot+=g//(i-1)+1\n",
    "                    return tot\n",
    "        else:\n",
    "            g=s2-s1\n",
    "            for i in range(6,1,-1):\n",
    "                if g>d2[i]*(i-1):\n",
    "                    g-=d2[i]*(i-1)\n",
    "                    tot+=d2[i]\n",
    "                else:\n",
    "                    if g%(i-1)==0:\n",
    "                        tot+=g//(i-1)\n",
    "                    else:\n",
    "                        tot+=g//(i-1)+1\n",
    "                    return tot\n",
    "                if g>(i-1)*d1[7-i]:\n",
    "                    g-=d1[7-i]*(i-1)\n",
    "                    tot+=d1[7-i]\n",
    "                else:\n",
    "                    if g%(i-1)==0:\n",
    "                        tot+=g//(i-1)\n",
    "                    else:\n",
    "                        tot+=g//(i-1)+1\n",
    "                    return tot\n",
    "        return tot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m, n = len(nums1), len(nums2)\n",
    "        if m > 6 * n or n > 6 * m:\n",
    "            return -1\n",
    "        \n",
    "        d = sum(nums2) - sum(nums1)\n",
    "        if d < 0:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "            d = -d\n",
    "        \n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "\n",
    "        ans = 0\n",
    "        for i in range(5, 0, -1):\n",
    "            if i * cnt[i] >= d:\n",
    "                return  ans + (d + i - 1) // i\n",
    "            ans += cnt[i]\n",
    "            d -= i * cnt[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        length_1 = len(nums1)\n",
    "        length_2 = len(nums2)\n",
    "\n",
    "        if length_1 > 6 * length_2 or length_2 > 6 * length_1:\n",
    "            return -1\n",
    "        \n",
    "        diff = sum(nums2) - sum(nums1)\n",
    "        if diff < 0:\n",
    "            diff = -diff\n",
    "            nums1, nums2 = nums2, nums1\n",
    "        \n",
    "        ans = 0\n",
    "        cnt = Counter(6-x for x in nums1) + Counter(x-1 for x in nums2)\n",
    "        for i in range(5, 0, -1):\n",
    "            if i * cnt[i] >= diff:\n",
    "                return ans + (diff + i -1) // i\n",
    "            ans += cnt[i]\n",
    "            diff -= i * cnt[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if 6*len(nums1)<len(nums2) or 6*len(nums2) <len(nums1):\n",
    "            return -1\n",
    "        d=sum(nums2)-sum(nums1)\n",
    "        if d<0:\n",
    "            d=-d\n",
    "            nums1,nums2=nums2,nums1\n",
    "        ans=0\n",
    "        cnt=Counter(6-x for x in nums1)+Counter(x - 1 for x in nums2)\n",
    "        for i in range(5,0,-1):\n",
    "            if i*cnt[i]>=d:\n",
    "                return ans+(d+i-1)//i\n",
    "            ans+=cnt[i]\n",
    "            d-=i*cnt[i]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        sum1, sum2 = sum(nums1), sum(nums2)\n",
    "        \n",
    "        # 判断长度是否满足条件\n",
    "        if min(len(nums1), len(nums2)) * 6 < max(len(nums1), len(nums2)):\n",
    "            return -1\n",
    "        \n",
    "        # 如果两数组和已经相等\n",
    "        if sum1 == sum2:\n",
    "            return 0\n",
    "        \n",
    "        # 将两数组排序\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        \n",
    "        # 确保 nums1 的和小于 nums2 的和\n",
    "        if sum1 > sum2:\n",
    "            nums1, nums2, sum1, sum2 = nums2, nums1, sum2, sum1\n",
    "        \n",
    "        diff = sum2 - sum1\n",
    "        count = 0\n",
    "        \n",
    "        # 使用双指针来找到最有效的操作\n",
    "        i, j = 0, len(nums2) - 1\n",
    "        while diff > 0:\n",
    "            # 计算替换 nums1[i] 或 nums2[j] 可以减少多少差异\n",
    "            gain1 = 6 - nums1[i] if i < len(nums1) else 0\n",
    "            gain2 = nums2[j] - 1 if j >= 0 else 0\n",
    "            \n",
    "            # 选择减少最多差异的操作\n",
    "            if gain1 > gain2:\n",
    "                diff -= gain1\n",
    "                i += 1\n",
    "            else:\n",
    "                diff -= gain2\n",
    "                j -= 1\n",
    "            \n",
    "            count += 1\n",
    "        \n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        cnt = [0] * 6\n",
    "        s1, s2 = sum(nums1), sum(nums2)\n",
    "        if s1 > s2:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "            s1, s2 = s2, s1\n",
    "        for x in nums1:\n",
    "            cnt[6 - x] += 1\n",
    "        for y in nums2:\n",
    "            cnt[y - 1] += 1\n",
    "        diff = s2 - s1\n",
    "        ans = 0\n",
    "        for i in range(5, 0, -1):\n",
    "            x = cnt[i]\n",
    "            if diff > i * x:\n",
    "                ans += x\n",
    "                diff -= i * x \n",
    "            else:\n",
    "                ans += (diff + i - 1) // i\n",
    "                return ans\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        # 贪心\n",
    "        n,m = len(nums1),len(nums2)\n",
    "        if 6*m < n or 6*n < m:\n",
    "            return -1\n",
    "        s1 = sum(nums1)\n",
    "        s2 = sum(nums2)\n",
    "        diff = s1 - s2\n",
    "        if s2 > s1:\n",
    "            nums1,nums2 = nums2,nums1\n",
    "            diff = -diff\n",
    "        # 默认nums1减少，nums2增加\n",
    "        cnt = Counter(x-1 for x in nums1) + Counter(6-x for x in nums2)\n",
    "        # 首先减少多的\n",
    "        ans = 0\n",
    "        for i in range(5,0,-1):\n",
    "            if cnt[i] * i >= diff:\n",
    "                return ans + (diff+i-1)//i\n",
    "            else:\n",
    "                diff -= cnt[i] * i\n",
    "                ans += cnt[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if 6 * len(nums1) < len(nums2) or 6 * len(nums2) < len(nums1):\n",
    "            return -1  # 优化\n",
    "        d = sum(nums2) - sum(nums1)  # 数组元素和的差，我们要让这个差变为 0\n",
    "        if d < 0:\n",
    "            d = -d\n",
    "            nums1, nums2 = nums2, nums1  # 统一让 nums1 的数变大，nums2 的数变小\n",
    "        ans = 0\n",
    "        # 统计每个数的最大变化量（nums1 的变成 6，nums2 的变成 1）\n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "        for i in range(5, 0, -1):  # 从大到小枚举最大变化量 5 4 3 2 1\n",
    "            if i * cnt[i] >= d:  # 可以让 d 变为 0\n",
    "                return ans + (d + i - 1) // i\n",
    "            ans += cnt[i]  # 需要所有最大变化量为 i 的数\n",
    "            d -= i * cnt[i]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "             \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "\n",
    "\n",
    "        def help(h1, h2, diff):\n",
    "\n",
    "            h = [0] * 7\n",
    "\n",
    "            for i in range(1, 7):\n",
    "                h[6 - i] += h1[i]\n",
    "                h[i - 1] += h2[i]\n",
    "\n",
    "            res = 0\n",
    "\n",
    "            for i in range(5, 0, -1):\n",
    "                if diff <= 0: break\n",
    "\n",
    "                t = min((diff + i - 1) // i, h[i])\n",
    "                res += t\n",
    "\n",
    "                diff -= t * i\n",
    "\n",
    "            return res\n",
    "\n",
    "\n",
    "        n, m = len(nums1), len(nums2)\n",
    "\n",
    "        if 6 * n < m or 6 * m < n: return -1\n",
    "\n",
    "        cnt1 = [0] * 7\n",
    "        cnt2 = [0] * 7\n",
    "\n",
    "        diff = 0\n",
    "        for i in nums1:\n",
    "            cnt1[i] += 1\n",
    "            diff += i\n",
    "        for i in nums2:\n",
    "            cnt2[i] += 1\n",
    "            diff -= i\n",
    "        if diff == 0 :\n",
    "            return 0\n",
    "        if diff > 0:\n",
    "            return help(cnt2, cnt1, diff)\n",
    "        return help(cnt1, cnt2, -diff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        # s1, s2 = sum(nums1), sum(nums2)\n",
    "        # if s1 == s2:\n",
    "        #     return 0\n",
    "        # nums1, nums2 = (nums1, nums2) if s1 < s2 else (nums2, nums1)\n",
    "        # changes = [6 - n for n in nums1] + [n - 1 for n in nums2]\n",
    "        # changes.sort(reverse=True)\n",
    "        # gap, count = abs(s1 - s2), 0\n",
    "        # for n in changes:\n",
    "        #     gap -= n\n",
    "        #     count += 1\n",
    "        #     if gap <= 0:\n",
    "        #         return count\n",
    "        # return -1\n",
    "        changes, gap = [0 for _ in range(6)], 0\n",
    "        for n in nums1:\n",
    "            changes[6 - n] += 1\n",
    "            gap -= n\n",
    "        for n in nums2:\n",
    "            changes[n - 1] += 1\n",
    "            gap += n\n",
    "        # print(gap, changes)\n",
    "        if gap == 0:\n",
    "            return 0\n",
    "        elif gap > 0:\n",
    "            res = 0\n",
    "            for i in range(5, -1, -1):\n",
    "                while changes[i] > 0:\n",
    "                    gap -= i\n",
    "                    changes[i] -= 1\n",
    "                    res += 1\n",
    "                    if gap <= 0:\n",
    "                        return res\n",
    "        else:\n",
    "            res = 0\n",
    "            for i in range(6):\n",
    "                while changes[i] > 0:\n",
    "                    gap += 5 - i\n",
    "                    changes[i] -= 1\n",
    "                    res += 1\n",
    "                    if gap >= 0:\n",
    "                        return res\n",
    "        return -1\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if len(nums1) > 6*len(nums2) or len(nums2) > 6*len(nums1):\n",
    "            return -1\n",
    "        diff = sum(nums1) - sum(nums2)\n",
    "        if diff == 0:\n",
    "            return 0\n",
    "        if diff < 0:\n",
    "            diff *= -1\n",
    "            nums1,nums2 = nums2,nums1\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        i = len(nums1) - 1\n",
    "        j = 0\n",
    "        res = 0\n",
    "        while diff > 0:\n",
    "            d1 = nums1[i] - 1\n",
    "            d2 = 6 - nums2[j]\n",
    "            if diff > 5 or diff > d1 and diff > d2:\n",
    "                if d1 > d2:\n",
    "                    diff -= d1\n",
    "                    i -= 1 if i != 0 else 0\n",
    "                else:\n",
    "                    diff -= d2\n",
    "                    j += 1 if j != len(nums2) - 1 else 0\n",
    "                res += 1\n",
    "            else:\n",
    "                res += 1\n",
    "                break\n",
    "        return res\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "\n",
    "        s1=sum(nums1)\n",
    "        s2=sum(nums2)\n",
    "        if s1==s2: return 0\n",
    "        \n",
    "        target=max(s1,s2)-min(s1,s2)\n",
    "        if s1<s2: nums1,nums2=nums2,nums1\n",
    "\n",
    "        if len(nums1)>6*len(nums2): return -1 #由Max-Min>Max-len(nums1)+6*len(nums2)-Min化简而来\n",
    "        p1,p2=len(nums1)-1,0\n",
    "        res=0\n",
    "        while target>0:\n",
    "            if nums1[p1]-1>=6-nums2[p2]:\n",
    "                target-=nums1[p1]-1\n",
    "                p1-=1\n",
    "            else:\n",
    "                target-=6-nums2[p2]\n",
    "                p2+=1\n",
    "            res+=1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if sum(nums1) < sum(nums2):\n",
    "            return self.minOperations(nums2, nums1)\n",
    "        \n",
    "        if len(nums1) * 1 > len(nums2) * 6:\n",
    "            return -1\n",
    "\n",
    "        diff = sum(nums1)- sum(nums2)\n",
    "\n",
    "        nums1.sort(reverse=True)\n",
    "        nums2.sort()\n",
    "        print(nums1)\n",
    "        print(nums2)\n",
    "        ans = 0\n",
    "        i = j = 0\n",
    "        while diff>0:\n",
    "            if j == len(nums2) or (i < len(nums1) and  nums1[i] - 1 > 6- nums2[j] ) :\n",
    "                diff -=  nums1[i] - 1\n",
    "                i += 1\n",
    "            else:\n",
    "                diff -=  6- nums2[j]\n",
    "                j += 1\n",
    "            ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter,defaultdict\n",
    "import math\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        len1,len2 = len(nums1),len(nums2)\n",
    "        sum1,sum2 = sum(nums1),sum(nums2)\n",
    "        if sum1==sum2: return 0\n",
    "        min_v1, max_v1 = len1, 6*len1\n",
    "        min_v2, max_v2 = len2, 6*len2\n",
    "        if min_v1>max_v2 or min_v2>max_v1:\n",
    "            return -1\n",
    "        cnt1,cnt2 = Counter(nums1),Counter(nums2)\n",
    "        big,small = None,None\n",
    "        big_sum,small_sum = 0,0\n",
    "        if sum1>sum2:\n",
    "            big,small=cnt1,cnt2\n",
    "            big_sum,small_sum=sum1,sum2\n",
    "        else:\n",
    "            big,small=cnt2,cnt1\n",
    "            big_sum,small_sum=sum2,sum1\n",
    "        diff = big_sum - small_sum\n",
    "        rec = defaultdict(int)\n",
    "        for i in range(1, 6):\n",
    "            rec[6-i] += (big[7-i] + small[i])\n",
    "        res = 0\n",
    "        for i in range(5, 0, -1):\n",
    "            if rec[i]>0:\n",
    "                if rec[i]*i>=diff:\n",
    "                    res += math.ceil(diff/i)\n",
    "                    return res\n",
    "                else:\n",
    "                    diff -= rec[i]*i\n",
    "                    res += rec[i]\n",
    "            pass    \n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        l1 = len(nums1)\n",
    "        l2 = len(nums2)\n",
    "        if l1 > l2 * 6 or l2 > l1 * 6:\n",
    "            return -1\n",
    "        \n",
    "        s1 = sum(nums1)\n",
    "        s2 = sum(nums2)\n",
    "\n",
    "        if s1 == s2:\n",
    "            return 0\n",
    "        \n",
    "        if s1 > s2:\n",
    "            ma = nums1\n",
    "            mi = nums2\n",
    "        else:\n",
    "            mi = nums1\n",
    "            ma = nums2\n",
    "        \n",
    "        ma_cnt = {}\n",
    "        for num in ma:\n",
    "            ma_cnt[num] = ma_cnt.get(num, 0) + 1\n",
    "        \n",
    "        mi_cnt = {}\n",
    "        for num in mi:\n",
    "            mi_cnt[num] = mi_cnt.get(num, 0) + 1\n",
    "        \n",
    "        diff = abs(s1 - s2)\n",
    "        res = 0\n",
    "        for num in range(5):\n",
    "            cnt = ma_cnt.get(6 - num, 0) + mi_cnt.get(num + 1, 0)\n",
    "            if cnt * (5 - num) < diff:\n",
    "                diff -= cnt * (5 - num)\n",
    "                res += cnt\n",
    "            else:\n",
    "                res += diff // (5 - num)\n",
    "                if diff % (5 - num) != 0:\n",
    "                    res += 1\n",
    "                break\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m, n = len(nums1), len(nums2)\n",
    "        if 6 * m < n or m > 6 * n:  return -1\n",
    "\n",
    "        list1 = [0] * 6\n",
    "        list2 = [0] * 6\n",
    "        for num in nums1:   list1[num-1] += 1\n",
    "        for num in nums2:   list2[num-1] += 1\n",
    "\n",
    "        total1, total2 = sum((i+1) * list1[i] for i in range(6)), sum((i+1) * list2[i] for i in range(6))\n",
    "        if total1 == total2:    return 0\n",
    "        if total1 > total2: list1, list2 = list2, list1\n",
    "\n",
    "        target = abs(total1 - total2)\n",
    "        i, j, cnt = 0, 5, 0\n",
    "        while target:\n",
    "            if target >= j * (list1[i] + list2[j]):\n",
    "                cnt += list1[i] + list2[j]\n",
    "                target -= j * (list1[i] + list2[j])\n",
    "                i += 1\n",
    "                j -= 1\n",
    "            else:\n",
    "                cnt += target // j\n",
    "                if target % j:  cnt += 1\n",
    "                target = 0\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #和相等，选择 任意 数组中的任意一个整数\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        if n1 < n2 and 6*n1<n2:\n",
    "            return -1\n",
    "        if n1 > n2 and 6*n2<n1:\n",
    "            return -1\n",
    "        sum1 = sum(nums1)\n",
    "        sum2 = sum(nums2)\n",
    "        d = sum(nums2) - sum(nums1)  # 数组元素和的差，我们要让这个差变为 0\n",
    "        if d == 0:\n",
    "            return 0\n",
    "        if d < 0:\n",
    "            d = -d\n",
    "            nums1, nums2 = nums2, nums1  # 统一让 nums1 的数变大，nums2 的数变小\n",
    "        ans = 0\n",
    "        cnt = Counter(6 - x for x in nums1) + Counter(x - 1 for x in nums2)\n",
    "\n",
    "        for i in range(5,0,-1):\n",
    "            if i*cnt[i] >= d:\n",
    "                return ans + math.ceil(d/i)\n",
    "            ans += cnt[i]  # 需要所有最大变化量为 i 的数\n",
    "            d -= i * cnt[i]\n",
    "        \n",
    "\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n1 = len(nums1)\n",
    "        mp1 = {}\n",
    "        sum1 = 0\n",
    "        for num in nums1:\n",
    "            if(num not in mp1):\n",
    "                mp1[num] = 1\n",
    "            else:\n",
    "                mp1[num] += 1\n",
    "            sum1 += num \n",
    "        \n",
    "        n2 = len(nums2)\n",
    "        mp2 = {}\n",
    "        sum2 = 0\n",
    "        for num in nums2:\n",
    "            if(num not in mp2):\n",
    "                mp2[num] = 1\n",
    "            else:\n",
    "                mp2[num] += 1\n",
    "            sum2 += num\n",
    "\n",
    "        if(sum1 == sum2):\n",
    "            return 0\n",
    "        if(n1 > 6*n2 or n2 > 6*n1):\n",
    "            return -1\n",
    "        left = max(n1,n2)\n",
    "        right = min(6*n1,6*n2)\n",
    "\n",
    "        def calcCnt(nums, tsum, mp, target):\n",
    "            if(tsum == target):\n",
    "                return 0\n",
    "            elif(tsum > target):\n",
    "                result = 0\n",
    "                for i in range(6,1,-1):\n",
    "                    if(i not in mp):\n",
    "                        continue\n",
    "                    if((i-1)*mp[i] >= tsum-target):\n",
    "                        if((tsum-target) % (i-1) == 0):\n",
    "                            result += (tsum-target) // (i-1)\n",
    "                        else:\n",
    "                            result += (tsum-target) // (i-1) + 1\n",
    "                        break\n",
    "                    else:\n",
    "                        tsum -= mp[i]*(i-1)\n",
    "                        result += mp[i]\n",
    "            else:\n",
    "                result = 0\n",
    "                for i in range(1,6):\n",
    "                    if(i not in mp):\n",
    "                        continue\n",
    "                    if((6-i)*mp[i] >= target-tsum):\n",
    "                        if((target-tsum) % (6-i) == 0):\n",
    "                            result += (target-tsum) // (6-i)\n",
    "                        else:\n",
    "                            result += (target-tsum) // (6-i) + 1\n",
    "                        break\n",
    "                    else:\n",
    "                        tsum += mp[i]*(6-i)\n",
    "                        result += mp[i]\n",
    "            return result\n",
    "        result = float('inf')\n",
    "\n",
    "        for target in range(left,right+1):\n",
    "            result = min(result, calcCnt(nums1,sum1,mp1,target)+calcCnt(nums2,sum2,mp2,target))\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s1 = sum(nums1)\n",
    "        s2 = sum(nums2)\n",
    "        if s1==s2:return 0\n",
    "        c1 = Counter(nums1)\n",
    "        c2 = Counter(nums2)\n",
    "        if s2 > s1:\n",
    "            c1, c2  = c2, c1\n",
    "        d = abs(s1-s2)\n",
    "        ans = 0\n",
    "        print(s1, s2 )\n",
    "       \n",
    "        for i in range(5, 0, -1):\n",
    "            \n",
    "            j  = c1[i+1] + c2[6-i]\n",
    "            print( i, j, d, ans)\n",
    "            if i*j >= d:\n",
    "                \n",
    "                ans += (d+i-1)//i\n",
    "                d = 0\n",
    "                break\n",
    "            else:\n",
    "                d -= i*j\n",
    "                ans +=j\n",
    "\n",
    "        if d>0:return -1\n",
    "        return ans\n",
    "        # 04:00 错 debug ->\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n1 = len(nums1)\n",
    "        mp1 = {}\n",
    "        sum1 = 0\n",
    "        for num in nums1:\n",
    "            if(num not in mp1):\n",
    "                mp1[num] = 1\n",
    "            else:\n",
    "                mp1[num] += 1\n",
    "            sum1 += num \n",
    "        \n",
    "        n2 = len(nums2)\n",
    "        mp2 = {}\n",
    "        sum2 = 0\n",
    "        for num in nums2:\n",
    "            if(num not in mp2):\n",
    "                mp2[num] = 1\n",
    "            else:\n",
    "                mp2[num] += 1\n",
    "            sum2 += num\n",
    "\n",
    "        if(sum1 == sum2):\n",
    "            return 0\n",
    "        if(n1 > 6*n2 or n2 > 6*n1):\n",
    "            return -1\n",
    "        left = max(n1,n2)\n",
    "        right = min(6*n1,6*n2)\n",
    "\n",
    "        def calcCnt(nums, tsum, mp, target):\n",
    "            if(tsum == target):\n",
    "                return 0\n",
    "            elif(tsum > target):\n",
    "                result = 0\n",
    "                for i in range(6,1,-1):\n",
    "                    if(i not in mp):\n",
    "                        continue\n",
    "                    if((i-1)*mp[i] >= tsum-target):\n",
    "                        if((tsum-target) % (i-1) == 0):\n",
    "                            result += (tsum-target) // (i-1)\n",
    "                        else:\n",
    "                            result += (tsum-target) // (i-1) + 1\n",
    "                        break\n",
    "                    else:\n",
    "                        tsum -= mp[i]*(i-1)\n",
    "                        result += mp[i]\n",
    "            else:\n",
    "                result = 0\n",
    "                for i in range(1,6):\n",
    "                    if(i not in mp):\n",
    "                        continue\n",
    "                    if((6-i)*mp[i] >= target-tsum):\n",
    "                        if((target-tsum) % (6-i) == 0):\n",
    "                            result += (target-tsum) // (6-i)\n",
    "                        else:\n",
    "                            result += (target-tsum) // (6-i) + 1\n",
    "                        break\n",
    "                    else:\n",
    "                        tsum += mp[i]*(6-i)\n",
    "                        result += mp[i]\n",
    "            return result\n",
    "        result = float('inf')\n",
    "\n",
    "        for target in range(left,right+1):\n",
    "            result = min(result, calcCnt(nums1,sum1,mp1,target)+calcCnt(nums2,sum2,mp2,target))\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if sum(nums1)==sum(nums2):\n",
    "            return 0\n",
    "        n=len(nums1)\n",
    "        m=len(nums2)\n",
    "        if max(n,m)>min(n,m)*6:\n",
    "            return -1\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        q1=deque(nums1)\n",
    "        q2=deque(nums2)\n",
    "        t1=sum(nums1)\n",
    "        t2=sum(nums2)\n",
    "        res=0\n",
    "        while t1!=t2:\n",
    "            if t1<t2:\n",
    "                t1,t2=t2,t1\n",
    "                q1,q2=q2,q1\n",
    "            res+=1\n",
    "            if q1 and q1[-1]-1>=t1-t2:\n",
    "                break\n",
    "            if q2 and 6-q2[0]>=t1-t2:\n",
    "                break\n",
    "            if len(q1)==0:\n",
    "                t2+=6-q2.popleft()\n",
    "            elif len(q2)==0:\n",
    "                t1-=q1.pop()-1\n",
    "            elif q1[-1]-1>6-q2[0]:\n",
    "                t1-=q1.pop()-1\n",
    "            else:\n",
    "                t2+=6-q2.popleft()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        s1, s2 = sum(nums1), sum(nums2)\n",
    "        if s1 == s2:\n",
    "            return 0\n",
    "        if s1 > s2:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "            s1, s2 = s2, s1\n",
    "        i, j, cnt  = 0, len(nums2) - 1, 0\n",
    "        while s1 < s2 and (i < len(nums1) or j > -1):\n",
    "            print(cnt, s1, s2, i, j)\n",
    "            cnt += 1\n",
    "            if i < len(nums1) and j > -1:\n",
    "                if 6 - nums1[i] > nums2[j] - 1:\n",
    "                    if s2 - s1 > 6 - nums1[i]:\n",
    "                        s1 += 6 - nums1[i] \n",
    "                        i += 1\n",
    "                    else:\n",
    "                        s1 = s2\n",
    "                else:\n",
    "                    if s2 - s1 > nums2[j] - 1:\n",
    "                        s2 -= nums2[j] - 1 \n",
    "                        j -= 1\n",
    "                    else:\n",
    "                        s1 = s2\n",
    "            elif i < len(nums1):\n",
    "                if s2 - s1 > 6 - nums1[i]:\n",
    "                    s1 += 6 - nums1[i] \n",
    "                    i += 1\n",
    "                else:\n",
    "                    s1 = s2\n",
    "            else:\n",
    "                if s2 - s1 > nums2[j] - 1:\n",
    "                    s2 -= nums2[j] - 1 \n",
    "                    j -= 1\n",
    "                else:\n",
    "                    s1 = s2\n",
    "        if s1 == s2:\n",
    "            return cnt\n",
    "        else:\n",
    "            return -1\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s1=sum(nums1)\n",
    "        s2=sum(nums2)\n",
    "        tmp=s2-s1\n",
    "        if s1>s2:\n",
    "            nums1,nums2=nums2,nums1\n",
    "            tmp=s1-s2\n",
    "        n=len(nums1)\n",
    "        for i in range(n):\n",
    "            nums1[i]=6-nums1[i]\n",
    "        for i in nums2:\n",
    "            nums1.append(i-1)\n",
    "        nums1.sort(reverse=True)\n",
    "\n",
    "        res=0\n",
    "        idx=0\n",
    "        # print(nums1)\n",
    "        # print(tmp)\n",
    "        while tmp>0 and idx<len(nums1):\n",
    "            res+=1\n",
    "            tmp-=nums1[idx]\n",
    "            idx+=1\n",
    "        return res if tmp<=0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s1 = sum(nums1)\n",
    "        s2 = sum(nums2)\n",
    "        if s1 == s2:\n",
    "            return 0\n",
    "        if s1 > s2:\n",
    "            s1, s2, nums1, nums2 = s2, s1, nums2, nums1\n",
    "        target = s2 - s1\n",
    "        nums1 = [6 - x for x in nums1]\n",
    "        nums2 = [x - 1 for x in nums2]\n",
    "        if sum(nums1) + sum(nums2) < target:\n",
    "            return -1\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        l1 = len(nums1) - 1\n",
    "        l2 = len(nums2) - 1\n",
    "        ret = 0\n",
    "        print(target, nums1, nums2)\n",
    "        while l1 >= 0 and l2 >= 0:\n",
    "            if nums1[l1] >= nums2[l2]:\n",
    "                k = nums1[l1]\n",
    "                l1 -= 1\n",
    "            else:\n",
    "                k = nums2[l2]\n",
    "                l2 -= 1\n",
    "            ret += 1\n",
    "            target -= k\n",
    "            if target <= 0:\n",
    "                return ret\n",
    "        if l1 >= 0:\n",
    "            l = l1\n",
    "            nums = nums1\n",
    "        else:\n",
    "            l = l2\n",
    "            nums = nums2\n",
    "        for i in range(l, -1, -1):\n",
    "            ret += 1\n",
    "            target -= nums[i]\n",
    "            if target <= 0:\n",
    "                return ret\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n,m = len(nums1), len(nums2)\n",
    "        if n < m and n * 6 < m * 1:\n",
    "            return -1 \n",
    "        if m < n and m * 6 < n * 1:\n",
    "            return -1  \n",
    "        s1 = sum(nums1)\n",
    "        s2 = sum(nums2)\n",
    "        d = abs(s2 - s1) \n",
    "        arr = []\n",
    "        if s1 < s2:\n",
    "            for x in nums1:\n",
    "                arr.append(6 - x)\n",
    "            for x in nums2:\n",
    "                arr.append(x - 1) \n",
    "        elif s1 > s2:\n",
    "            for x in nums2:\n",
    "                arr.append(6 - x)\n",
    "            for x in nums1:\n",
    "                arr.append(x - 1)\n",
    "        else:\n",
    "            return 0 \n",
    "        arr.sort() \n",
    "        ans = 0 \n",
    "        i = len(arr)-1\n",
    "        while d > 0:\n",
    "            d -= arr[i]\n",
    "            ans += 1\n",
    "            i-=1\n",
    "        return ans \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        sum1, sum2 = sum(nums1), sum(nums2)\n",
    "        diff = abs(sum1 - sum2)\n",
    "        if sum1 == sum2:\n",
    "            return 0\n",
    "        elif sum1 > sum2:\n",
    "            larger, smaller = nums1, nums2\n",
    "        else:\n",
    "            larger, smaller = nums2, nums1\n",
    "        larger = [-x for x in larger]\n",
    "        larger.sort()\n",
    "        smaller.sort()\n",
    "        operation = 0\n",
    "        while diff != 0:\n",
    "            if larger[0] == -1 and smaller[0] == 6:\n",
    "                return -1\n",
    "            if (-1 - larger[0]) > (6 - smaller[0]):\n",
    "                l = heapq.heappop(larger)\n",
    "                diff -= min((-1 - l, diff))\n",
    "                heapq.heappush(larger, -1)\n",
    "            else:\n",
    "                s = heapq.heappop(smaller)\n",
    "                diff -= min((6 - s, diff))\n",
    "                heapq.heappush(smaller, 6)\n",
    "            operation += 1\n",
    "        return operation\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s1=sum(nums1)\n",
    "        s2=sum(nums2)\n",
    "        tmp=s2-s1\n",
    "        if s1>s2:\n",
    "            nums1,nums2=nums2,nums1\n",
    "            tmp=s1-s2\n",
    "        n=len(nums1)\n",
    "        for i in range(n):\n",
    "            nums1[i]=6-nums1[i]\n",
    "        for i in nums2:\n",
    "            nums1.append(i-1)\n",
    "        nums1.sort(reverse=True)\n",
    "\n",
    "        res=0\n",
    "        idx=0\n",
    "        print(nums1)\n",
    "        print(tmp)\n",
    "        while tmp>0 and idx<len(nums1):\n",
    "            res+=1\n",
    "            tmp-=nums1[idx]\n",
    "            idx+=1\n",
    "        return res if tmp<=0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "      ln1, ln2 = len(nums1), len(nums2)\n",
    "      if 6*ln1 < ln2 or 6*ln2 < ln1:\n",
    "        return -1\n",
    "      cnt1, cnt2 = sum(nums1), sum(nums2)\n",
    "      mx, mn = 6, 1\n",
    "      re , merged_pq = 0, []\n",
    "      if cnt1 < cnt2:\n",
    "        tep = nums1\n",
    "        nums1, nums2 = nums2, tep\n",
    "      for x in nums1:\n",
    "        if x > mn: heapq.heappush(merged_pq, 1-x)\n",
    "      for y in nums2:\n",
    "        if y < mx: heapq.heappush(merged_pq, y-6)\n",
    "      ans = abs(cnt2 - cnt1)\n",
    "      while ans > 0 and merged_pq:\n",
    "          # print(cntx,cntm,nmx,nmm)\n",
    "          re += 1\n",
    "          ans += heapq.heappop(merged_pq)\n",
    "      return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s1, s2 = sum(nums1), sum(nums2)\n",
    "        if s1 == s2:\n",
    "            return 0\n",
    "        nums1, nums2 = (nums1, nums2) if s1 < s2 else (nums2, nums1)\n",
    "        changes = [6 - n for n in nums1] + [n - 1 for n in nums2]\n",
    "        changes.sort(reverse=True)\n",
    "        gap, count = abs(s1 - s2), 0\n",
    "        for n in changes:\n",
    "            gap -= n\n",
    "            count += 1\n",
    "            if gap <= 0:\n",
    "                return count\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if len(nums1) > len(nums2):\n",
    "            nums1, nums2 = nums2, nums1\n",
    "\n",
    "        m, n = len(nums1), len(nums2)\n",
    "        if m * 6 < n:\n",
    "            return -1\n",
    "\n",
    "        if sum(nums1) > sum(nums2):\n",
    "            nums1, nums2 = nums2, nums1\n",
    "        \n",
    "        q1, q2 = [], []\n",
    "        for num in nums1:\n",
    "            if num != 6:\n",
    "                heapq.heappush(q1, num)\n",
    "        for num in nums2:\n",
    "            if num != 1:\n",
    "                heapq.heappush(q2, -num)\n",
    "\n",
    "        d = sum(nums2) - sum(nums1)\n",
    "        if not d:\n",
    "            return 0\n",
    "        ans = 0\n",
    "        while q1 or q2:\n",
    "            if not q1:\n",
    "                x = -heapq.heappop(q2)\n",
    "                if x - 1 >= d:\n",
    "                    return ans + 1\n",
    "                d -= (x - 1)\n",
    "            elif not q2:\n",
    "                x = heapq.heappop(q1)\n",
    "                if 6 - x >= d:\n",
    "                    return ans + 1\n",
    "                d -= (6 - x)\n",
    "            elif (6 - q1[0]) >= (-q2[0] - 1):\n",
    "                x = heapq.heappop(q1)\n",
    "                if 6 - x >= d:\n",
    "                    return ans + 1\n",
    "                d -= (6 - x)\n",
    "            else:\n",
    "                x = -heapq.heappop(q2)\n",
    "                if x - 1 >= d:\n",
    "                    return ans + 1\n",
    "                d -= (x - 1)\n",
    "\n",
    "            ans += 1\n",
    "            if not d:\n",
    "                return ans\n",
    "        \n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        sum1 = sum(nums1)\n",
    "        sum2 = sum(nums2)\n",
    "        \n",
    "        count = 0\n",
    "        \n",
    "        if sum1 > sum2:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "            sum1, sum2 = sum2, sum1\n",
    "            \n",
    "        diff = sum2-sum1\n",
    "        aa =[]\n",
    "        bb= []\n",
    "        \n",
    "        for i in nums1:\n",
    "            if i != 6:\n",
    "                aa.append(6-i)\n",
    "        for i in nums2:\n",
    "            if i != 1:\n",
    "                bb.append(i-1)\n",
    "                \n",
    "        aa += bb\n",
    "        aa.sort()\n",
    "        \n",
    "        while diff >0 and aa:\n",
    "            diff -= aa.pop()\n",
    "            count += 1\n",
    "            \n",
    "        return count if diff <= 0 else -1\n",
    "            \n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        sum1 = sum(nums1)\n",
    "        sum2 = sum(nums2)\n",
    "        \n",
    "        count = 0\n",
    "        \n",
    "        if sum1 > sum2:\n",
    "            nums1, nums2 = nums2, nums1\n",
    "            sum1, sum2 = sum2, sum1\n",
    "            \n",
    "        diff = sum2-sum1\n",
    "        aa =[]\n",
    "        bb= []\n",
    "        \n",
    "        for i in nums1:\n",
    "            if i != 6:\n",
    "                aa.append(6-i)\n",
    "        for i in nums2:\n",
    "            if i != 1:\n",
    "                bb.append(i-1)\n",
    "                \n",
    "        aa += bb\n",
    "        aa.sort()\n",
    "        \n",
    "        while diff >0 and aa:\n",
    "            diff -= aa.pop()\n",
    "            count += 1\n",
    "            \n",
    "        return count if diff <= 0 else -1\n",
    "            \n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s1 = sum(nums1)\n",
    "        s2 = sum(nums2)\n",
    "        if s1 > s2:\n",
    "            s1, s2 = s2, s1\n",
    "            nums1, nums2 = nums2, nums1\n",
    "        target = s2 - s1\n",
    "        diff = []\n",
    "        for num in nums1:\n",
    "            diff.append(6 - num)\n",
    "        for num in nums2:\n",
    "            diff.append(num - 1)\n",
    "        diff = sorted(diff, reverse=True)\n",
    "        ans = 0\n",
    "        for d in diff:\n",
    "            if target <= 0:\n",
    "                break\n",
    "            target -= d\n",
    "            ans += 1\n",
    "        return -1 if target > 0 else ans\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "      cnt1, cnt2 = sum(nums1), sum(nums2)\n",
    "      mx, mn = 6, 1\n",
    "      re= 0\n",
    "      if cnt1 > cnt2:\n",
    "        merged_pq = [mn-x for x in nums1 if x > mn] + [x-mx for x in nums2 if x < mx]  \n",
    "      else:\n",
    "        merged_pq = [mn-x for x in nums2 if x > mn] + [x-mx for x in nums1 if x < mx] \n",
    "      ans = abs(cnt2 - cnt1)\n",
    "      heapq.heapify(merged_pq)\n",
    "      while ans > 0 and merged_pq:\n",
    "          # print(cntx,cntm,nmx,nmm)\n",
    "          re += 1\n",
    "          ans -= min(ans,-heapq.heappop(merged_pq))\n",
    "      return re if ans==0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "      cnt1, cnt2 = sum(nums1), sum(nums2)\n",
    "      mx, mn = 6, 1\n",
    "      re= 0\n",
    "      if cnt1 > cnt2:\n",
    "        merged_pq = [mn-x for x in nums1 if x > mn] + [x-mx for x in nums2 if x < mx]  \n",
    "      else:\n",
    "        merged_pq = [mn-x for x in nums2 if x > mn] + [x-mx for x in nums1 if x < mx] \n",
    "      ans = abs(cnt2 - cnt1)\n",
    "      heapq.heapify(merged_pq)\n",
    "      while ans > 0 and merged_pq:\n",
    "          # print(cntx,cntm,nmx,nmm)\n",
    "          re += 1\n",
    "          ans += heapq.heappop(merged_pq)\n",
    "      return re if ans<=0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if len(nums1)>len(nums2):\n",
    "            nums1,nums2=nums2,nums1\n",
    "        if len(nums1)*6<len(nums2):return -1\n",
    "\n",
    "        if sum(nums1)<sum(nums2):\n",
    "            can=[6-nums1[i] for i in range(len(nums1))]+[nums2[i]-1 for i in range(len(nums2))]\n",
    "            can.sort(key=lambda x:-x)\n",
    "            need=sum(nums2)-sum(nums1)\n",
    "            temp=0\n",
    "            for i in range(len(can)):\n",
    "                temp+=can[i]\n",
    "                if temp>=need:return i+1\n",
    "\n",
    "\n",
    "        elif sum(nums1)>sum(nums2):\n",
    "            nums1,nums2=nums2,nums1\n",
    "\n",
    "            can=[6-nums1[i] for i in range(len(nums1))]+[nums2[i]-1 for i in range(len(nums2))]\n",
    "            can.sort(key=lambda x:-x)\n",
    "            need=sum(nums2)-sum(nums1)\n",
    "            temp=0\n",
    "            for i in range(len(can)):\n",
    "                temp+=can[i]\n",
    "                if temp>=need:return i+1\n",
    "\n",
    "\n",
    "        else:return 0\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s1, s2 = sum(nums1), sum(nums2)\n",
    "        if s1 == s2:\n",
    "            return 0\n",
    "        if s1 > s2:\n",
    "            return self.minOperations(nums2, nums1)\n",
    "        arr = [6 - v for v in nums1] + [v - 1 for v in nums2]\n",
    "        d = s2 - s1\n",
    "        for i, v in enumerate(sorted(arr, reverse=True), 1):\n",
    "            d -= v\n",
    "            if d <= 0:\n",
    "                return i\n",
    "        return -1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
